package com.hp.docker_base.service.impl;

import com.alibaba.fastjson.JSON;
import com.hp.docker_base.algorithm.BRBInference3;
import com.hp.docker_base.bean.*;
import com.hp.docker_base.mapper.StomachMapper;
import com.hp.docker_base.util.ReadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StomachService {

    @Autowired
    ReadUtil readUtil;

    @Autowired
    StomachMapper stomachMapper;

    private ArrayList<ArrayList<Double>> reference;
    private ArrayList<ArrayList<Double>> belief;

    public void Init() {
        Map map = readUtil.Stomach_dataRead();
        reference = (ArrayList<ArrayList<Double>>) map.get("reference");
        belief = (ArrayList<ArrayList<Double>>) map.get("belief");
    }

    public StomachFidOutDto CalculateRes(String RecId,
                                         String DeptId,
                                         String DataIn) {
        Init();
        List<DataInDto> dataInDtos = JSON.parseArray(DataIn, DataInDto.class);
        ArrayList<Double> input = new ArrayList<>();
        for (int i = 0; i < dataInDtos.size(); i++) {
            input.add(Double.parseDouble(dataInDtos.get(i).getValue()));
        }
        ArrayList<ArrayList<Double>> Input = new ArrayList<>();
        Input.add(input);


        BRBInference3 brbInference3 = new BRBInference3(reference, belief, Input);
        brbInference3.Init();
        brbInference3.Process();
        brbInference3.fusion();

        ArrayList<ArrayList<Double>> activation_weight = brbInference3.getActivation_weight();
        ArrayList<ArrayList<Double>> m_xin = brbInference3.getM_xin();

        List<ActivedRulesDto> ActivedRules = new ArrayList<>();
        for(int i=0; i<activation_weight.get(0).size(); i++){
            ActivedRulesDto temp = new ActivedRulesDto(i + 1, activation_weight.get(0).get(i));
            ActivedRules.add(temp);
        }

        List<DataOutDto> Result = new ArrayList<>();
        for(int i=0; i<m_xin.get(0).size(); i++){
            if(i<9){
                DataOutDto temp = new DataOutDto("sk0" + (i + 1), m_xin.get(0).get(i));
                Result.add(temp);
            } else {
                DataOutDto temp = new DataOutDto("sk" + (i + 1), m_xin.get(0).get(i));
                Result.add(temp);
            }
        }
        ArrayList<StomachRule> allData = stomachMapper.getAllData();
        StomachFidOutDto stomachFidOutDto = new StomachFidOutDto();
        stomachFidOutDto.setActivedRules(ActivedRules);
        stomachFidOutDto.setResult(Result);
        stomachFidOutDto.setDeptId(DeptId);
        stomachFidOutDto.setRecId(RecId);
        stomachFidOutDto.setStomachRules(allData);
        return stomachFidOutDto;

    }

    public List<String> updateService(String newRule){
        Init();
        List<StomachRule> stomachRules = JSON.parseArray(newRule, StomachRule.class);
        List<String> tips = new ArrayList<>();
        for(StomachRule stomachRule : stomachRules) {

            ArrayList<Double> single = new ArrayList<>();
            single.add(stomachRule.getId01());
            single.add(stomachRule.getId02());
            single.add(stomachRule.getId03());
            single.add(stomachRule.getId04());
            single.add(stomachRule.getId05());
            single.add(stomachRule.getId06());
            single.add(stomachRule.getId07());
            single.add(stomachRule.getId08());
            single.add(stomachRule.getId09());

            int index = -1;
            boolean flag = true;
            for (int i = 0; i < reference.size() && flag; i++) {
                int count = 0;
                for (int j = 0; j < reference.get(i).size(); j++) {
                    if (Math.abs(single.get(j) - reference.get(i).get(j)) < 0.001) {
                        count += 1;
                        if (count == single.size()) {
                            index = i;
                            flag = false;
                        }
                    }
                }
            }

            if (index == -1) {
                stomachRule.setRule(reference.size() + 1);
                stomachMapper.InsertSingleRule(stomachRule);
                tips.add("已添加第" + stomachRule.getRule() + "条规则") ;
            } else {
                stomachRule.setRule(index + 1);
                stomachMapper.updateRule(stomachRule);
                tips.add("已覆盖第" + stomachRule.getRule() + "条规则") ;
            }
        }
        return tips;
    }

    public String deleteRule(int id){
        Init();
        if(id > reference.size()){
            return "输入的规则序号超出引索范围";
        }else {
            stomachMapper.deleteRule(id);
            stomachMapper.addSelf(id);
            return "已删除第"+id+"条规则";
        }
    }

    public List<Map<String, Object>> showRules(){
        List<Map<String, Object>> list = new ArrayList<>();
        ArrayList<StomachRule> allData = stomachMapper.getAllData();
        for(int i=0; i<allData.size(); i++){
            Map<String, Object> map = new HashMap<>();
            map.put("Rid", i + 1);
            map.put("rule", allData.get(i));
            list.add(map);
        }
        return list;
    }

}
